package com.hitechbangla.fctu.base.weather;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import resource_builder.GenericTypeBuilder;

/**
 *
 * @author Asus
 */
public class Wind implements Serializable
{
    private WindLayer[] layerArray;
    private String name;
    

    public WindLayer getLayer(int i)
    {
	return layerArray[i];
    }
    
    
    public Wind()
    {
	initializeArray();
    } // end of constructor
    
    public Wind(float[] h, String name)
    {
	this.name = name;
	initializeArray();
	setLayerLimit(h);
    }
    
    public int getLayerIndexOnHeight(double height)
    {
	for(int i=0;i<layerArray.length;i++)
	{
	    if(height>layerArray[i].getHeightLimitLower() && height<=layerArray[i].getHeightLimitUpper())
	    {
		return i;
	    }
	}
	return -1;
    }
    
    private void initializeArray()
    {
	layerArray = new WindLayer[7];
	
	for(int i=0;i<7;i++)
	{
	    layerArray[i] = new WindLayer();
	}
    }
    
    
    private void setLayerLimit(float[] h)
    {	
	// setting the upperlimit of each layer
	for(int i=0;i<layerArray.length;i++)
	{
	    layerArray[i].setHeightLimitUpper(h[i]);
	}
	
	// setting the lower limit of each layer
	
	layerArray[0].setHeightLimitLower(0);
	
	for(int i=1;i<layerArray.length;i++)
	{
	    layerArray[i].setHeightLimitLower(h[i-1]);
	}
	
    } // end of setLayerLimit
    
    
    public void setWindSpeedAndHeading(float[] speedArray, float[] headingArray)
    {
        for(int i=0;i<speedArray.length;i++)
        {
            layerArray[i].setWindSpeed(speedArray[i]);
        }
        
        for(int i=0;i<headingArray.length;i++)
        {
            layerArray[i].setWindHeading(headingArray[i]);
        }
    }
    
    
    
    public static List<Wind> getAllWind()
    {
        GenericTypeBuilder<Wind> genericTypeBuilder = new GenericTypeBuilder<>();
        List<Wind> listLayerCollection = genericTypeBuilder.readAll("windCollection.object");
	return listLayerCollection;
    }

    /**
     * @return the name
     */
    public String getName() {
	return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) 
    {
	this.name = name;
    }
    
    // this method is for test purpose
    public static void main(String a[])
    {
	List<Wind> list = Wind.getAllWind();
	
	for(Wind wind:list)
	{
	    //System.out.println(wind.getName());
	}
    }
    //
}


